﻿<?php
class Image 
{
    private $name;
	private $type;
    private $width;
	private $height;
	
    public function __construct($img_path)
    {
        $this->name = $img_path;
		$this->type = pathinfo($this->name, PATHINFO_EXTENSION);
		
    }
	public function __set($name,$value)
	{
		$this->{$name} = $value;
	} 
	public function __get($name)
	{
		return $this->{$name};
	}
	// ====== Image Dimensions ============== //
	public function getImageD($filename ='')
	{
		// Get new sizes
		list($width, $height) = getimagesize($this->name);
		$size['W']= $width;
		$size['H']= $height;
		return $size;	   
	}
	
//============ Image Zoom =================== // 
	public function resize($save_path,$width,$height)
	{ 
		// File and new size
		$filename = $this->name;
		$percent = 0.5;
		// Content type
		//header('Content-type: image/png');
		$extension = pathinfo($filename, PATHINFO_EXTENSION);
		switch($extension){
			case 'jpg':
			{
				 // Get new sizes
			   list($oldwidth, $oldheight) = getimagesize($filename);
			   $newwidth = $width;     //$oldwidth * $percent;
			   $newheight = $height;   //$oldheight * $percent;
			  // Load
			   $thumb =  imagecreatetruecolor($newwidth, $newheight);
			   $source = imagecreatefromjpeg($filename);
			   // Resize
			   imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight);
			  // Output
			   if(imagejpeg($thumb,$save_path)){
				 return true; 
			   } else
				  return false;
			}
			break;
			case 'png':
			{
				// Get new sizes
			   list($oldwidth, $oldheight) = getimagesize($filename);
			   $newwidth = $width;   // $oldwidth * $percent;
			   $newheight = $height;  //$oldheight * $percent;
			  // Load
			   $thumb = imagecreatetruecolor($newwidth, $newheight);
			   $source = imagecreatefrompng($filename);
			   // Resize
			   imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight);
			  // Output
			   if(imagepng($thumb,$save_path)){
				  return true; 
			   } else
				  return false;
			 }
			 break;
			case 'gif':
			{
				// Get new sizes
			   list($oldwidth, $oldheight) = getimagesize($filename);
			   $newwidth = $width;    // $oldwidth * $percent;
			   $newheight = $height;  // $oldheight * $percent;
			   // Load
			   $thumb = imagecreatetruecolor($newwidth, $newheight);
			   $source = imagecreatefromgif($filename);
			   // Resize
			   imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight);
			   // Output
			   if(imagegif($thumb,$save_path)){
				  return true; 
			   }
			   else
				  return false;
			 }
			 break;
			 default :
			 break;
		 }
	}   
///////////////////////////////////////////////
	public function createImage($params)
	{
		$img_tmp = imagecreatetruecolor($params['width'], $params['height']);// Load New Image with Path
		//list($img_w, $img_h) = getimagesize($this->name);// Get new sizes
		switch($this->type){
			case 'jpg':
			{	
				$resource = imagecreatefromjpeg($this->name);
				return $resource;				
			}
			break;
			case 'png':
			{
				$resource = imagecreatefrompng($this->name);
				return $resource;
			}
			break;
			case 'gif':
			{
				$resource = imagecreatefromgif($this->name);
				return $resource;
			}
			break;
			default :
			break;
		}
	}
	function saveImage($resource,$save_path){
		switch($this->type){
			case 'jpg':
			{
			   if(imagejpeg($resource,$save_path)){// Output
				 return true; 
			   } else
				  return false;
			}
			break;
			case 'png':
			{
			   if(imagepng($resource,$save_path)){
				  return true; 
			   } else
				  return false;
			 }
			 break;
			case 'gif':
			{
			   if(imagegif($resource,$save_path)){
				  return true; 
			   }
			   else
				  return false;
			 }
			 break;
			 default :
			 break;
		 }
	}
	/** =============================================
	* image filters
	*===============
	*
	**/
	function imageFilteration($effect,$params)
	{
		$image = $this->createImage($params);
		switch($effect){
			case 1: imageconvolution($image, array(array(-1, -1, -1),array(-1, 16, -1), array(-1, -1, -1)), 8, 0);
			break;
			case 2: imagefilter($image,IMG_FILTER_GAUSSIAN_BLUR); break;
			case 3: imagefilter($image,IMG_FILTER_BRIGHTNESS, 50); break;
			case 4: imagefilter($image,IMG_FILTER_BRIGHTNESS, -20); break;
			case 5: imagefilter($image,IMG_FILTER_CONTRAST, -20); break;
			case 6: imagefilter($image,IMG_FILTER_CONTRAST, 20); break;
			case 7: imagefilter($image,IMG_FILTER_GRAYSCALE); break;
			case 8: imagefilter($image,IMG_FILTER_NEGATE); break;
			case 9: imagefilter($image,IMG_FILTER_COLORIZE, 128, 0, 0, 50); break;
			case 10: imagefilter($image,IMG_FILTER_COLORIZE, 0, 128, 0, 50); break;
			case 11: imagefilter($image,IMG_FILTER_COLORIZE, 0, 0, 128, 50); break;
			case 12: imagefilter($image,IMG_FILTER_EDGEDETECT); break;
			case 13: imagefilter($image,IMG_FILTER_EMBOSS); break;
			case 14: imagefilter($image,IMG_FILTER_MEAN_REMOVAL); break;
			default :
				return false;
		}
			return $this->saveImage($image,$params['destination']);
	}
	/** ================================
	* image crop
	============
	* 		$image = imagecreatefromjpeg("photo.jpg");
			$copy = PIPHP_ImageCrop($image, 100, 0, 110, 140);
			if ($copy != FALSE){
				header("Content-type: image/jpeg");
				imagejpeg();
			}
	*
	**/
	function imageCrop($image, $x, $y, $w, $h)
	{
		$tw = imagesx($image);
		$th = imagesy($image);
		if ($x > $tw || $y > $th || $w > $tw || $h > $th)
			return false;
		$temp = imagecreatetruecolor($w, $h);
		imagecopyresampled($temp, $image, 0, 0, $x, $y, $w, $h, $w, $h);
		return $temp;
	}
	function PIPHP_ImageResize($image, $w, $h)
	{
		$oldw = imagesx($image);
		$oldh = imagesy($image);
		$temp = imagecreatetruecolor($w, $h);
		imagecopyresampled($temp, $image, 0, 0, 0, 0,$w, $h, $oldw, $oldh);
		return $temp;
	}
	function PIPHP_MakeThumbnail($image, $max)
	{
		$thumbw = $w = imagesx($image);
		$thumbh = $h = imagesy($image);
		if ($w > $h && $max < $w){
			$thumbh = $max / $w * $h;
			$thumbw = $max;
		} elseif ($h > $w && $max < $h){
			$thumbw = $max / $h * $w;
			$thumbh = $max;
		} elseif ($max < $w) {
			$thumbw = $thumbh = $max;
		}
		return PIPHP_ImageResize($image, $thumbw, $thumbh);
	}
	function PIPHP_ImageConvert($fromfile, $tofile, $type, $quality)
	{
		$contents = file_get_contents($fromfile);
		$image = imagecreatefromstring($contents);
		switch($type)
		{
			case "gif": imagegif($image, $tofile); break;
			case "jpeg": imagejpeg($image, $tofile, $quality); break;
			case "png": imagepng($image, $tofile,
			round(9 - $quality * .09)); break;
		}
	}
	function PIPHP_GifText($file, $text, $font, $size, $fore, $back,
	$shadow, $shadowcolor)
	{
		$bound = imagettfbbox($size, 180, $font, $text);
		$width = $bound[2] + $bound[0] + 6 + $shadow;
		$height = abs($bound[1]) + abs($bound[7]) + 5 + $shadow;
		$image = imagecreatetruecolor($width, $height);
		$bgcol = PIPHP_GD_FN1($image, $back);
		$fgcol = PIPHP_GD_FN1($image, $fore);
		$shcol = PIPHP_GD_FN1($image, $shadowcolor);
		imagefilledrectangle($image, 0, 0, $width, $height, $bgcol);
		if ($shadow > 0) 
		imagettftext($image, $size, 180, $shadow + 2,abs($bound[5]) + $shadow + 2, $shcol, $font, $text);
		imagettftext($image, $size, 180, 2, abs($bound[5]) + 2, $fgcol,$font, $text);
		imagegif($image, $file);
	}
	function PIPHP_GD_FN1($image, $color)
	{
		return imagecolorallocate($image,
									hexdec(substr($color, 0, 2)),
									hexdec(substr($color, 2, 2)),
									hexdec(substr($color, 4, 2)));
	}
	/**
	*  PIPHP_ImageWatermark("pic.jpg", "wmark.png", "png", 75,"Watermark", "oldenglish.ttf", 90, "ffffff", 2);
	*
	**/
	function PIPHP_ImageWatermark($fromfile, $tofile, $type,
		$quality, $text, $font, $size, $fore, $opacity)
		{
		$contents = file_get_contents($fromfile);
		$image1 = imagecreatefromstring($contents);
		$bound = imagettfbbox($size, 0, $font, $text);
		$width = $bound[2] + $bound[0] + 6;
		$height = abs($bound[1]) + abs($bound[7]) + 5;
		$image2 = imagecreatetruecolor($width, $height);
		$bgcol = $this->PIPHP_GD_FN1($image2, "fedcba");
		$fgcol = $this->PIPHP_GD_FN1($image2, $fore);
		imagecolortransparent($image2, $bgcol);
		imagefilledrectangle($image2, 0, 0, $width, $height, $bgcol);
		imagettftext($image2, $size, 0, 2, abs($bound[5]) + 2,$fgcol, $font,$text);
		imagecopymerge($image1, $image2,(imagesx($image1) - $width) / 2,
										(imagesy($image1) - $height) / 2,
						0, 0, $width, $height, $opacity);
		switch($type){
		case "gif": imagegif($image1, $tofile); break;
		case "jpeg": imagejpeg($image1, $tofile, $quality); break;
		case "png": imagepng($image1, $tofile,round(9 - $quality * .09)); break;
		}
	}
	function textConvert($text)
	{
		// Convert UTF-8 string to HTML entities
		$text = mb_convert_encoding($text, 'HTML-ENTITIES',"UTF-8");
		// Convert HTML entities into ISO-8859-1
		$text = html_entity_decode($text,ENT_NOQUOTES, "ISO-8859-1");
		// Convert characters > 127 into their hexidecimal equivalents
		$out = "";
		for($i = 0; $i < strlen($text); $i++) {
			$letter = $text[$i];
			$num = ord($letter);
			if($num>127) {
			  $out .= "&#$num;";
			} else {
			  $out .=  $letter;
			}
		}
		return $out;
	}
	//============================= //
	function wm()
	{
		//http://www.php.net/manual/en/image.examples-png.php
		// Load the stamp and the photo to apply the watermark to
		$stamp = imagecreatefrompng('stamp.png');
		$im = imagecreatefromjpeg('photo.jpeg');

		// Set the margins for the stamp and get the height/width of the stamp image
		$marge_right = 10;
		$marge_bottom = 10;
		$sx = imagesx($stamp);
		$sy = imagesy($stamp);

		// Copy the stamp image onto our photo using the margin offsets and the photo 
		// width to calculate positioning of the stamp. 
		imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

		// Output and free memory
		header('Content-type: image/png');
		imagepng($im);
		imagedestroy($im);
	}
	
	
	
	
	
	
	
	
}

?>